﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace YahtzeeAspTest.Model
{  

    public class Game
    {
        public List<Player> ListOfPlayer { get; set; }
        public int Round { get; set; }
        public Dice[] TableOfDice { get; set; }
        public bool IsFinish { get; set; }
        public bool IsPause { get; set; }
        const int MAX_ROUND = 1000;
        private int _currentPlayer = 0;
        private static Game mInstance;

        public static Game Instance
        {
            get
            {
                if (mInstance == null)
                {
                    //throw new Exception("Object not created");
                    return null;
                }
                else
                {
                    return mInstance;
                }
            }        
        }
        public static void Create (Dice[] dice, List<Player> listofplayer)
        {
            if (mInstance != null)
            {
                //throw new Exception("Object already created");
            }
            else
            {

                mInstance = new Game(dice, listofplayer);
            }
        }

        private Game()
        {
            
        }

        private Game(Dice[] dice, List<Player> listofplayer)
        {
            IsPause = false;
            IsFinish = false;
            TableOfDice = dice;
            ListOfPlayer = listofplayer;
            Round = 0;
        }

        public void throwingDice()
        {           
            if (Round < MAX_ROUND)
            {               
                if (ListOfPlayer[_currentPlayer].alreadyDoAChoose)
                {
                    _currentPlayer++;
                    ListOfPlayer[_currentPlayer].alreadyDoAChoose = false;
                }
                foreach (var dice in TableOfDice)
                {
                    dice.Roll();
                }
                if (!ListOfPlayer[_currentPlayer].canIThrowAgainADice())
                {
                    foreach (var dice in TableOfDice)
                    {
                        dice.Locked = false;
                    }
                    SiteMaster.enableButton();
                    _currentPlayer++;
                }                
                if (_currentPlayer == ListOfPlayer.Count())
                {
                    Round++;
                    _currentPlayer = 0;
                }
            }
            else
            {
                IsFinish = true;
                gameFinish();
            }
           /* if (Round < MAX_ROUND)
            {
                    if (_currentPlayer == ListOfPlayer.Count())
                    {
                        Round++;
                        _currentPlayer = 0;
                    }
                    else if (ListOfPlayer[_currentPlayer].alreadyDoAChoose)
                    {
                        _currentPlayer++;
                        ListOfPlayer[_currentPlayer].alreadyDoAChoose = false;
                    }
                    else
                    {
                        if (ListOfPlayer[_currentPlayer].canIThrowAgainADice())
                        {
                            foreach (var dice in TableOfDice)
                            {
                                dice.Roll();
                            }
                        }
                        else
                        {
                            foreach (var dice in TableOfDice)
                            {
                                dice.Locked = false;
                            }
                            _currentPlayer++;
                        }
                    }
                }
                else
                {
                    IsFinish = true;
                    gameFinish();
                }*/            
        }

        public void putPause()
        {
            IsPause = IsPause ? false : true;
        }

        public void lockThisDice(int i)
        {
            TableOfDice[i].lockADice();
            TableOfDice[i].ImageValue = "~/DiceLocked" + TableOfDice[i].Value + ".png";
        }

        private void gameFinish()
        {
            ListOfPlayer = ListOfPlayer.OrderBy(x => x.Score).ToList();
            ListOfPlayer[0].IsWinner = true;
            for (int i = 1; i < ListOfPlayer.Count(); i++)
            {
                ListOfPlayer[i].IsWinner = false;
            }
        }

    }
}